﻿using System.Web;
using System.Web.UI;
using SiteUnity.Frame.Common.UITools.Interface;
using SiteUnity.Frame.Common.Tools.Class;

namespace SiteUnity.Frame.Common.UITools
{
    public delegate void onClick();

    public enum View
    {
        Default,
        Grid,
        List
    }

    public class UITools
    {
        public UITools() { }

        static public void isClicked(Button button)
        {
            if (isExists(button))
                button.onClick();
        }

        static public string GetFormValue(Control control)
        {
            if (isExists(control))
            {
                string value = HttpContext.Current.Request.Form[control.UniqueID];

                if (typeof(ITitleable).IsAssignableFrom(control.GetType()))
                    if ((string)ClassTools.GetProperty(control,"Title") == value)
                        //if (control is TextBox)
                        //    if (((TextBox)control).Title == value)
                        return string.Empty;

                return value;
            }
            return string.Empty;
        }

        static private bool isExists(Control control)
        {
            if (HttpContext.Current.Request.Form[control.UniqueID] != null)
                return true;
            else
                return false;
        }
    }

    public class UIValidation
    {
        static public bool isValid(Control[] form)
        {
            foreach (Control item in form)
                if (typeof(IValidateable).IsAssignableFrom(item.GetType()))
                {
                    if (!(bool)ClassTools.GetProperty(item, "isValid"))
                        return false;
                }

            return true;
        }
    }

    //static public Control Generate(List<FormItem> form, bool showLabel)
    //{
    //    HtmlTable table = new HtmlTable();

    //    foreach (var item in form)
    //    {
    //        HtmlTableRow row = new HtmlTableRow();

    //        if (showLabel)
    //        {
    //            HtmlTableCell cell = new HtmlTableCell();
    //            cell.InnerText = item.Control.ID;
    //            row.Cells.Add(cell);
    //        }
    //        HtmlTableCell cell2 = new HtmlTableCell();
    //        cell2.Controls.Add(item.Control);
    //        row.Cells.Add(cell2);


    //        table.Rows.Add(row);
    //    }

    //    return table;
    //}
}
